/**
 * @file displaymdi.h
 * @brief
 *
 *
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_DISPLAY_DISPLAYMDI_H_
#define MYBOOK_DISPLAY_DISPLAYMDI_H_

#include "childframeweb.h"
#include "displaytabctrl.h"

typedef CWinTraits<WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0> CDisplayMdiTraits;

class CDisplayMdi
	: public CWindowImpl<CDisplayMdi, CWindow, CDisplayMdiTraits>
	, CDoubleBufferImpl<CDisplayMdi>
{
public:
	DECLARE_WND_CLASS(_T("MyDisplayMdi"))

	CDisplayMdi()
		: m_pMediator(NULL)
		, m_pActiveChildFrame(NULL)
	{
	}

	HWND CreateClient(HWND hParent, CDisplayTabCtrl* pTabCtrl)
	{
		CWindowRect rect(hParent);
		rect.MoveToXY(0, 0);

		return Create(hParent, rect);
	}

	HWND CreateFrame(LPCTSTR lpszUrl)
	{
		CChildFrame* pChildFrame = GetChildFrame(lpszUrl);
		if (pChildFrame != NULL)
		{
			ExecuteShowFrame(pChildFrame);

			if (m_pMediator)
				m_pMediator->ChangedAddTab(pChildFrame);
		}

		return pChildFrame->m_hWnd;
	}

public:
	BEGIN_MSG_MAP(CDisplayMdi)
		CHAIN_MSG_MAP(CDoubleBufferImpl<CDisplayMdi>)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		MESSAGE_HANDLER(WM_SETTEXT, OnSetText)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	void DoPaint(CDCHandle dc)
	{
	}

	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		CChildFrame* pChildFrame = m_pActiveChildFrame;
		if (pChildFrame != NULL)
			ExecuteShowFrame(pChildFrame);

		return 0;
	}

	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		m_vecChildFrame.clear();

		return 0;
	}

	LRESULT OnSetText(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
	{
		if (wParam && lParam)
		{
			CChildFrame* pChildFrame = (CChildFrame*)wParam;
			LPCTSTR lpszTitle = (LPCTSTR)lParam;

			m_pMediator->ChangedTitle(pChildFrame, lpszTitle);
		}

		return 0;
	}

public:
	CChildFrame* FindChildFrame(LPCTSTR lpszUrl)
	{
		std::vector<CChildFrame*>::const_iterator it = m_vecChildFrame.begin();
		for (; it != m_vecChildFrame.end(); ++it)
		{
			CChildFrameSnb* pChildFrame = dynamic_cast<CChildFrameSnb*>(*it);
			if (pChildFrame && _tcsicmp(pChildFrame->GetUrl(), lpszUrl) == 0)
				return pChildFrame;
		}

		return NULL;
	}

	CChildFrame* GetChildFrame(LPCTSTR lpszUrl)
	{
		CChildFrame* pChildFrame = NULL;

		pChildFrame = new CChildFrameSnb();
		(dynamic_cast<CChildFrameSnb*>(pChildFrame))->CreateFrame(m_hWnd, lpszUrl);

		if (pChildFrame != NULL)
			m_vecChildFrame.push_back(pChildFrame);

		return pChildFrame;
	}

	void ExecuteShowFrame(CChildFrame* pChildFrame)
	{
		if (pChildFrame != NULL)
		{
			std::vector<CChildFrame*>::const_iterator it = m_vecChildFrame.begin();
			for (; it != m_vecChildFrame.end(); ++it)
			{
				if ((*it) && (*it != pChildFrame))
					(*it)->Show(false);
			}

			pChildFrame->Show(true);
			m_pActiveChildFrame = pChildFrame;
		}
	}

	void ExecuteQuitView(CChildFrame* pChildFrame, CChildFrame* pActive)
	{
		if (pChildFrame != NULL)
		{
			m_vecChildFrame.erase(std::remove(m_vecChildFrame.begin(), m_vecChildFrame.end(), pChildFrame));
			pChildFrame->PostMessage(WM_CLOSE);

			if (m_pActiveChildFrame == pChildFrame)
				m_pActiveChildFrame = NULL;
		}

		if (pActive != NULL)
		{
			ExecuteShowFrame(pActive);
		}
	}

public:
	void SetMediator(CDisplayMediator* pMediator)
	{
		m_pMediator = pMediator;
	}

	void ChangedShow(CChildFrame* pChildFrame)
	{

	}

	void ChangedClose(CChildFrame* pChildFrame)
	{

	}

protected:
	CDisplayMediator* m_pMediator;

protected:
	std::vector<CChildFrame*> m_vecChildFrame;
	CChildFrame* m_pActiveChildFrame;
};

#endif // MYBOOK_DISPLAY_DISPLAYMDI_H_